package com.weme.message.ext

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.view.View
import android.view.animation.DecelerateInterpolator
import com.chad.library.adapter.base.animation.ItemAnimator
import com.google.gson.Gson
import com.weme.chat.bean.CallMessage
import com.weme.chat.bean.GiftMessage
import com.weme.common.util.AgoraClient
import com.weme.message.MessageFragment
import io.agora.chat.ChatMessage
import io.agora.chat.CustomMessageBody
import io.agora.chat.TextMessageBody

class MessageExt {}
fun MessageFragment.getMessageContent(chatMessage: ChatMessage): String {
    return when (chatMessage.type) {
        ChatMessage.Type.TXT -> (chatMessage.body as? TextMessageBody)?.message ?: ""
        ChatMessage.Type.IMAGE -> getString(com.example.resources.R.string.chat_message_image)
        ChatMessage.Type.VIDEO -> getString(com.example.resources.R.string.chat_message_call)
        ChatMessage.Type.CUSTOM -> {
            when ((chatMessage.body as CustomMessageBody).event()) {
                AgoraClient.EVENT_TYPING_GIFT -> {
                    val params = (chatMessage.body as CustomMessageBody).params
                    val jsonString = Gson().toJson(params)
                    val giftMessage = Gson().fromJson(jsonString, GiftMessage::class.java)
                    getString(com.example.resources.R.string.chat_message_gift, giftMessage.giftName)
                }
                AgoraClient.EVENT_TYPING_CALL -> {
                    var content = ""
                    val params = (chatMessage.body as CustomMessageBody).params
                    val jsonString = Gson().toJson(params)
                    val callMessage = Gson().fromJson(jsonString, CallMessage::class.java)
                    val formatString = getString(com.example.resources.R.string.chat_message_call_msg)
                    if (callMessage.isMatching) { //如果是匹配电话
                        content = String.format(formatString, requireActivity().getText(com.example.resources.R.string.chat_match_back)) + "," + callMessage.callTime + "s"
                    } else {
                        if (callMessage.callTime == 0L) {
                            content = String.format(formatString, requireActivity().getText(com.example.resources.R.string.chat_call_missed))
                        } else {
                            content = String.format(formatString, requireActivity().getText(com.example.resources.R.string.chat_call_private)) + "," + callMessage.callTime + "s"
                        }
                    }
                    content
                } //   AgoraClient.EVENT_TYPING_NO_RESPONSE ->   getString(com.example.resources.R.string.chat_message_gift)
                else -> "未知"
            }
        }
        else -> ""
    }
}

class CustomAnimation1 : ItemAnimator {
    override fun animator(view: View): Animator { // 创建三个动画
        val alpha: Animator = ObjectAnimator.ofFloat(view, "alpha", 0f, 1f)
        val scaleY: Animator = ObjectAnimator.ofFloat(view, "scaleY", 1.3f, 1f)
        val scaleX: Animator = ObjectAnimator.ofFloat(view, "scaleX", 1.3f, 1f)

        scaleY.interpolator = DecelerateInterpolator()
        scaleX.interpolator = DecelerateInterpolator()

        // 多个动画组合，可以使用 AnimatorSet 包装
        val animatorSet = AnimatorSet()
        animatorSet.duration = 350
        animatorSet.play(alpha).with(scaleX).with(scaleY)
        return animatorSet
    }
}